package com.example.tys.refresh;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;

import smartRefresh.RefreshLayout;
import smartRefresh.listen.OnLoadMoreListener;
import smartRefresh.listen.OnRefreshListener;

public class MainActivity extends AppCompatActivity {

    private RecyclerView.Adapter adapter;
    private ArrayList<String> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i=0; i< 80; i++){
            data.add(String.valueOf(i));
        }
        RecyclerView rv = (RecyclerView) findViewById(R.id.recyclerView);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerView.Adapter<ViewHolder>() {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null, false);
                return new ViewHolder(view);
            }


            @Override
            public void onBindViewHolder(MainActivity.ViewHolder holder, int position) {
                holder.viewById.setText(data.get(position));
            }

            @Override
            public int getItemCount() {
                return data.size();
            }
        };
        rv.setAdapter(adapter);


        RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //传入false表示刷新失败
                Toast.makeText(MainActivity.this, "下拉",Toast.LENGTH_SHORT).show();
                refreshlayout.finishRefresh(2000);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                //传入false表示加载失败
                Toast.makeText(MainActivity.this, "上拉",Toast.LENGTH_SHORT).show();
                refreshlayout.finishLoadMore(2000);
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView viewById;
        public ViewHolder(View itemView) {
            super(itemView);
            viewById= (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
